package org.edu.modules.stu.biye.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.dto.StatusDto;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.mapper.VeBaseStudentMapper;
import org.edu.modules.stu.biye.mapper.VeStuGraduationMapper;
import org.edu.modules.stu.biye.entity.VeStuGraduation;
import org.edu.modules.stu.biye.dto.VeStuGraduationDto;
import org.edu.modules.stu.biye.service.VeStuGraduationService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 实现类
 */
@Service
public class VeStuGraduationServiceImpl extends BaseServiceImpl<VeStuGraduation, VeStuGraduationMapper> implements VeStuGraduationService {

    @Autowired
    private VeBaseStudentMapper veBaseStudentMapper;

    public IPage<VeStuGraduationDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper) {
        return mapper.pageDto(page, queryWrapper);
    }


    public VeStuGraduationDto stuDto(String stuid) {
        return mapper.stuDto(stuid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchGraduate(StatusDto statusDto) {
        LambdaUpdateWrapper<VeStuGraduation> updateWrapper = new LambdaUpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            // 步骤1：更新毕业表状态
            updateWrapper.set(VeStuGraduation::getJsxym, statusDto.getStatus());
            updateWrapper.in(VeStuGraduation::getId, statusDto.getIds());
            if (statusDto.getStatus() == 1) {
                // 毕业
                updateWrapper.set(VeStuGraduation::getJsxyReson, "正常毕业");
            } else if (statusDto.getStatus() == 2) {
                //结业
                updateWrapper.set(VeStuGraduation::getJsxyReson, "正常结业");
            }
            mapper.update(null, updateWrapper);
            // 步骤2：获取关联的学生ID
            List<Integer> stuIds = mapper.selectBatchIds(statusDto.getIds())
                    .stream()
                    .map(VeStuGraduation::getStuId)
                    .collect(Collectors.toList());
            // 步骤3：更新学生表状态
            LambdaUpdateWrapper<VeBaseStudent> stuWrapper = new LambdaUpdateWrapper<>();
            if (statusDto.getStatus() == 1) {
                // 毕业
                stuWrapper.set(VeBaseStudent::getXsdqztm, "BY");
            } else if (statusDto.getStatus() == 2) {
                //结业
                stuWrapper.set(VeBaseStudent::getXsdqztm, "JY");
            }
            stuWrapper.in(VeBaseStudent::getId, stuIds);
            veBaseStudentMapper.update(null, stuWrapper);
        }
    }

    /**
     * 查询实体类
     */
    public VeStuGraduationDto findDtoById(String id) {
        VeStuGraduation entity = super.findById(id);
        VeStuGraduationDto dto = ConvertUtils.convertToDto(entity, VeStuGraduationDto.class);
        return dto;
    }

    /**
     * 新增或者修改
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdate(VeStuGraduationDto dto, Boolean isAdd) {
        if (isAdd) {
            super.insert(dto);
        } else {
            super.update(dto);
            //毕业，修改学生信息为毕业
            //根据stuId获取学生数据
            VeBaseStudent veBaseStudent = veBaseStudentMapper.selectById(dto.getStuId());
            //结束学业码（1=>毕业，2=>结业，3=>未结业，4=>肄业）
            //当前状态码'XS'=>'新生', 'ZX' => '在校', 'XX' => '休学', 'TX' => '退学', 'KC' => '开除', 'BY' => '毕业', 'YY' => '肄业', 'ZXX' => '转学', 'JY' => '结业'"
            if (dto.getJsxym().equals("1")) {
                veBaseStudent.setXsdqztm("BY");
            } else if (dto.getJsxym().equals("2")) {
                veBaseStudent.setXsdqztm("JY");
            } else if (dto.getJsxym().equals("3")) {
                //veBaseStudent.setXsdqztm("JY");
            } else if (dto.getJsxym().equals("4")) {
                veBaseStudent.setXsdqztm("YY");
            }
            veBaseStudentMapper.updateById(veBaseStudent);
        }
    }

    @Override
    public void importExcel(List<Map<String, Object>> mapList) {
        for (Map map : mapList) {
            VeStuGraduationDto dto = new VeStuGraduationDto();
            //         dto.setStuId ((String)map.get(0000));
            //         dto.setFalId ((String)map.get(0000));
            //         dto.setFalName ((String)map.get(0000));
            //         dto.setSpecId ((String)map.get(0000));
            //         dto.setSpecName ((String)map.get(0000));
            //         dto.setGradeId ((String)map.get(0000));
            //         dto.setBjId ((String)map.get(0000));
            //         dto.setBjName ((String)map.get(0000));
            //         dto.setIdcard ((String)map.get(0000));
            //         dto.setCode ((String)map.get(0000));
            //         dto.setName ((String)map.get(0000));
            //         dto.setSex ((String)map.get(0000));
            //         dto.setCsDate ((String)map.get(0000));
            //         dto.setRxDate ((String)map.get(0000));
            //         dto.setXz ((String)map.get(0000));
            //         dto.setXzName ((String)map.get(0000));
            //         dto.setJsxyDate ((String)map.get(0000));
            //         dto.setJsxym ((String)map.get(0000));
            //         dto.setJsxyReson ((String)map.get(0000));
            //         dto.setByZsh ((String)map.get(0000));
            //         dto.setXwZsh ((String)map.get(0000));
            //         dto.setJyZsh ((String)map.get(0000));
            //         dto.setByDate ((String)map.get(0000));
            //         dto.setXwDate ((String)map.get(0000));
            //         dto.setJyDate ((String)map.get(0000));
            //         dto.setBookPrintTimes ((String)map.get(0000));
            //         dto.setPreByzsh ((String)map.get(0000));

            super.insert(dto);
        }

    }

}
